﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace Kz.Telegram.Bot.Infrastructure.Helpers
{
    public static class AesEncryptHelper
    {
        private static string ascKey = "A663549757348ECF1BFAB76CDCD6B76A";

        /// <summary>
        /// Encrypts the specified asc key.
        /// </summary>
        /// <param name="ascKey">The asc key.</param>
        /// <param name="toEncrypt">To encrypt.</param>
        /// <returns></returns>
        public static string AesEncrypt(this string toEncrypt)
        {
            byte[] keyArray = Encoding.UTF8.GetBytes(ascKey);
            byte[] toEncryptArray = Encoding.UTF8.GetBytes(toEncrypt);
            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = rDel.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        /// <summary>
        /// Decrypts the specified asc key.
        /// </summary>
        /// <param name="ascKey">The asc key.</param>
        /// <param name="toDecrypt">To decrypt.</param>
        /// <returns></returns>
        public static string AesDecrypt(this string toDecrypt)
        {
            byte[] keyArray = Encoding.UTF8.GetBytes(ascKey);
            byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = rDel.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Encoding.UTF8.GetString(resultArray);
        }
    }
}